package com.campusify.service.impl;

import org.springframework.stereotype.Service;
import com.campusify.compents.PageResult;
import com.campusify.pojo.CommentsExample;
import com.campusify.pojo.CommentsExample.Criteria;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.campusify.mapper.CommentsMapper;
import com.campusify.pojo.Comments;
import com.campusify.service.CommentsService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

/**
 * 服务实现层
 * @author Administrator
 *
 */
@Service
public class CommentsServiceImpl implements CommentsService {

	@Autowired
	private CommentsMapper commentsMapper;
	
	/**
	 * 查询全部
	 */
	@Override
	public List<Comments> findAll() {
		return commentsMapper.selectByExample(null);
	}

	/**
	 * 按分页查询
	 */
	@Override
	public PageResult findPage(int pageNum, int pageSize) {
		PageHelper.startPage(pageNum, pageSize);		
		Page<Comments> page=   (Page<Comments>) commentsMapper.selectByExample(null);
		return new PageResult(page.getTotal(), page.getResult());
	}

	/**
	 * 增加
	 */
	@Override
	public void add(Comments comments) {
		commentsMapper.insert(comments);		
	}

	
	/**
	 * 修改
	 */
	@Override
	public void update(Comments comments){
		commentsMapper.updateByPrimaryKey(comments);
	}	
	
	/**
	 * 根据ID获取实体
	 * @param id
	 * @return
	 */
	@Override
	public Comments findOne(Integer commentID){
		return commentsMapper.selectByPrimaryKey(commentID);
	}

	/**
	 * 批量删除
	 */
	@Override
	public void delete(Integer[] commentIDs) {
		for(Integer commentID:commentIDs){
			commentsMapper.deleteByPrimaryKey(commentID);
		}		
	}
	
	
		@Override
	public PageResult findPage(Comments comments, int pageNum, int pageSize) {
		PageHelper.startPage(pageNum, pageSize);
		
		CommentsExample example=new CommentsExample();
		Criteria criteria = example.createCriteria();
		
		if(comments!=null){			
						if(comments.getContent()!=null && comments.getContent().length()>0){
				criteria.andContentLike("%"+comments.getContent()+"%");
			}	
		}
		
		Page<Comments> page= (Page<Comments>)commentsMapper.selectByExample(example);		
		return new PageResult(page.getTotal(), page.getResult());
	}
	
}
